//
//  WBStatusListDAL.swift
//  Weibo
//
//  Created by cc on 11/8/17.
//  Copyright © 2017 cc. All rights reserved.
//

import Foundation



/// DAL - Data Access Layer  数据访问层
// 使命： 负责处理数据库和网络数据， 给ListViewModel 返回微博的[字典数组]
class WBStatusListDAL {
    
    
    /// 从本地数据库或者网络加载数据
    ///
    /// - Parameters:
    ///   - since_id: 下拉刷新id
    ///   - max_id: 上拉刷新id
    ///   - completion: 完成回调
    class func loadStatus(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping (_ list: [[String: Any]]?, _ isSUccess: Bool) -> ()) {
        
        // 0. 获取用户代号
        guard let userId  = WBNetworkManager.shared.userAccount.uid else {
            return
        }
        
        //1. 检查本地数据， 如果有，直接返回
        let array = CZSQLiteManager.shared.loadStatus(userId: userId, since_id: since_id, max_id: max_id)
        
        //判断数组的数量，没有数据返回的是没有数据的空数组[]
        if array.count > 0 {
            completion(array, true)
            
            return
        }
        
        //2. 加载网络数据
        
        WBNetworkManager.shared.statusList(since_id: since_id, max_id: max_id) { (list, isSuccess) in
            
            //判断网络请求是否成功
            if !isSuccess {
                completion(nil, false)
                return
            }
            
            //判断数据
            guard let list = list else {
                completion(nil, isSuccess)
                return
            }
            
            //3. 加载完成之后， 将网络数据[字典数组]，同步写入数据库
            CZSQLiteManager.shared.updateStatus(userId: userId, array: list)
            
            
            //4. 返回网络数据
            completion(list, isSuccess)
        }
        
    }
}
